<?PHP
defined('_XR_EXEC') or die();
/**
 * 控制器处理类 
 */
class account_cart_Controller extends controller_user1{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','购物车');
		$this->assign('account_menu1','2');
	}
	
	##################################
	/* 购物车数量 */
	################################## 
	function loadcartnum__(){
		$user_id = factory::module('member')->getuserid();
		$nums = factory::module('cart')->getCartNums($user_id);
		exit(''.$nums);
	}
	
	
	##################################
	/* 购物车 */
	################################## 
	function index__(){
		$m_order = factory::module('order');

		$user_id = factory::module('member')->getuserid();
		$rs_cart = factory::module('cart')->renewGetCart($user_id);
		$this->assign('rs_cart',$rs_cart);
		
		
		$comboids = array();
		foreach($rs_cart as $v){
			$comboids[] = $v['combo_id'];
		}
		$combosendarea = $m_order->combosSendArea($comboids);
		$this->assign('combosendarea',$combosendarea);
		
		//debug::show($combosendarea);
	} 
	
	##################################
	/* 清空购物车 */
	################################## 
	function deleteall__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$db = factory::getDBO();
			$user_id = factory::module('member')->getuserid();
			$db->query('update t_cart set deleted=1 where user_id="'.$user_id.'"');
			exit('{success:1}');
		}
		exit();
	}
	##################################
	/* 删除某一个购物 */
	################################## 
	function deleteone__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$_POST=format::addslash_deep($_POST);
			$id = $_POST['id'];
			$db = factory::getDBO();
			$user_id = factory::module('member')->getuserid();
			$db->query('update t_cart set deleted=1 where user_id="'.$user_id.'" and id="'.$id.'" ');
			exit('{success:1}');
		}
		exit();
	
	}
	##################################
	/* 更新购物车数量 */
	################################## 
	function updatenum__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$_POST=format::addslash_deep($_POST);
			$cart_id = intval($_POST['id']);
			$nums = intval($_POST['nums']);
			$user_id = factory::module('member')->getuserid();
			$m_cart = factory::module('cart');
			if(!$m_cart->updatenum($user_id,$cart_id,$nums)){
				exit('操作失败');
			}
			exit('{success:1}');
		}
		exit();
	
	}
	
	##################################
	/* 确认订单 */
	################################## 
	function confirmdeal__(){ 
		$db = factory::getdbo();
		$m_order = factory::module('order');
		$user_id = factory::module('member')->getuserid();
		$rs_cart = factory::module('cart')->renewGetCart($user_id);
		$balance = factory::module('trans')->getbalance($user_id);
		$this->assign('rs_cart',$rs_cart);
		if(count($rs_cart) <= 0){
			url::msgalert('购物车没有任何结算商品，请先进行选菜！','/account_buy/index');
		}
		
		$pricetotal = 0;
		$fenshu = 0;
		foreach($rs_cart as $v){
			$pricetotal += $v['price_total'];
			$one_fenshu = 0;
			if($v['combo_type'] == 0){
				foreach($v['combo_item'] as $val){
					$one_fenshu += $val['vege_num'];
				}
			}else{
				foreach($v['cart_item'] as $vals){
					$one_fenshu += $vals['vege_num'];
				}
			}
			$fenshu += $one_fenshu*$v['nums'];
		}
	 //	debug::show($user_id,$fenshu,$pricetotal);
		# 计算运费
		$yunfee = $m_order->caclyunfee($user_id,$fenshu,$pricetotal);
		if($yunfee === false){
			url::msgalert('您暂未设置配送方式，请联系我们的客服为您操作！','/account_buy/index');
		}
		$this->assign('yunfee',$yunfee);
		$this->assign('fenshu',$fenshu);
		$this->assign('pricetotal',$pricetotal);
		$this->assign('balance',$balance);
		
		 
		# 可配送日期
		$comboids = array();
		foreach($rs_cart as $v){
			$comboids[] = $v['combo_id'];
		}
		$combosendarea = $m_order->combosSendArea($comboids);
		if($combosendarea === false){
			url::msgalert('您当前选择的菜品，可配送时间段不一致，请先选择部分菜品下单！','/account_cart/index');
		}
		
		$rec_date = $m_order->combosSendArea($comboids,true);
		$this->assign('rec_date',$rec_date);

		# 配送时间段
		$this->assign('sendtime',factory::module('list')->getlist('sendtime'));
	} 
	##################################
	/* 提交订单 */
	################################## 
	function submitorder__(){ 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$_POST=format::addslash_deep($_POST);
			$rec_date =  $_POST['rec_date'];
			$sendtime =  $_POST['sendtime'];
		  
		 
			
			$m_order = factory::module('order');
			$user_id = factory::module('member')->getuserid();
			$rs_cart = factory::module('cart')->renewGetCart($user_id);
			
			#验证配送日期
			$comboids = array();
			foreach($rs_cart as $v){
				$comboids[] = $v['combo_id'];
			}
			$combosendarea = $m_order->combosSendArea($comboids);
			if($combosendarea === false){
				exit('您选择的配送日期有误，请重新刷新页面操作！');
			}
      if(!$m_order->isInCombosSendArea($comboids,$rec_date)){
				exit('您选择的配送日期有误，请重新刷新页面操作');
			}

			$goods = array();
 			foreach($rs_cart as $v){
				$tmp = array(
					'combo_id'=>$v['combo_id'],
					'nums'=>$v['nums'],
					'cart_item'=>array()
				);
				if($v['combo_type'] == '1'){
					foreach($v['cart_item'] as $val){
						$tmp['cart_item'][] = array(
							'vege_id'=>$val['vege_id'],
							'vege_num'=>$val['vege_num']
						);
					}
				}	
				$goods[]=$tmp;
			}
			$ret = $m_order->submitOrder($user_id,$goods,$user_id,true,$rec_date,$sendtime);
			if(!$ret){
				exit('操作失败：'.$m_order->getError()); 
			}
			exit('{success:1,order_id:"'.$ret['id'].'",trans_balance:"'.$ret['trans_balance'].'",trans_total:"'.$ret['trans_total'].'",point_total:"'.$ret['point_total'].'",point_balance:"'.$ret['point_balance'].'",ordercode:"'.$ret['code'].'",price_total:"'.$ret['price_total'].'"}');
			 
			
			
		}
		exit();
	} 

	 
	 
}
?>